//
//  Login2View.swift
//  CleanArchitecture
//
//  Created by siri on 2024/11/7.
//  Copyright © 2024 Tuan Truong. All rights reserved.
//

import SwiftUI
import  Combine
import SwiftUIRefresh
import Factory
struct LoginView: View {

    // 定义事件触发器类
    final class Triggers: ObservableObject {
        var load = PassthroughSubject<Void, Never>()
        var reload = PassthroughSubject<Void, Never>()
        var select = PassthroughSubject<Void, Never>()
        var login = PassthroughSubject<Void, Never>()
        var checkPatten = PassthroughSubject<Void, Never>()
    }

    @StateObject var viewModel: LoginViewModel
    @StateObject var output: LoginViewModel.Output
    @StateObject var cancelBag: CancelBag
    @StateObject var triggers: Triggers
    @EnvironmentObject var localeManager:LocaleManager // 多语言管理
    
    
    init(viewModel: LoginViewModel) {
        let triggers = Triggers()
        let cancelBag = CancelBag()
        
        let input = LoginViewModel.Input(
            loadTrigger: triggers.load.eraseToAnyPublisher(),
            reloadTrigger: triggers.reload.eraseToAnyPublisher(),
            selectTrigger: triggers.select.eraseToAnyPublisher(),
            loginTrigger: triggers.login.eraseToAnyPublisher(),
            checkPattenTrigger:triggers.checkPatten.eraseToAnyPublisher()
        )
       
        let output = viewModel.transform(input, cancelBag: cancelBag)
//        triggers.load.send()
      
        self._viewModel = StateObject(wrappedValue: viewModel)
        self._output = StateObject(wrappedValue: output)
        self._cancelBag = StateObject(wrappedValue: cancelBag)
        self._triggers = StateObject(wrappedValue: triggers)
    }
    
    var body: some View {
        VStack {
                NavigationView {
                    ZStack(){
                        VStack( spacing: 15) {
                            Spacer().frame(height: 32)
                            
                            //LOGO
                            HStack {
                                Spacer()
                                Image("pic_login_logo")
                                    .resizable()
                                    .scaledToFit()
                                    .frame(width: 240, height: 120)
                                Spacer()
                            }
                            
                         
                            
                            // 账号输入框
                            VStack (alignment: .leading){
                                // 账号
                                Text("usernameText")
                                    .font(Font.g_font_h1_regular20)
                                    .foregroundColor(Color.g_color_font_primary)
                                    
                                TextField("请输入账号", text: $output.username)
                                    .foregroundColor(Color.g_color_font_primary)
                                    .padding(.leading, 16)
                                    .frame(width: 280, height: 40)
                                    .background(Color.g_color_bg_grey)
                                    .cornerRadius(20)
                            }.padding()
                          
                            
                           
                            
                            // 密码输入框
                            VStack(alignment: .leading) {
                                // 密码
                                Text("passwordText")
                                    .font(Font.g_font_h1_regular20)
                                    .foregroundColor(Color.g_color_font_primary)
                               
                                SecureField("请输入密码", text: $output.password)
                                    .foregroundColor(Color.g_color_font_primary)
                                    .padding(.leading, 16)
                                    .frame(width: 280, height: 40)
                                    .background(Color.g_color_bg_grey)
                                    .cornerRadius(20)
                            }
                     
                            
                            // 忘记密码
                            HStack {
                                Spacer()
                                Button(action: {
                                    output.showForgotPasswordSheet = true
                                }) {
                                    Text("忘记密码")
                                        .font(Font.g_font_description_regular14)
                                        .foregroundColor(Color.g_color_font_secondary)
                                }
                                .padding(.trailing, 64)
                            }
                            
                            Spacer().frame(height: 4)
                            
                            // 立即登录按钮
                            VStack {
                                Button(action: {
                                    localeManager.changeLanguage()
                                }){
                                    Text("修改语言")
                                }
                                Button(action: {
                                    // 检查格式
                                    triggers.checkPatten.send()
                                    
                                    // 格式检验通过才发送请求
                                    if output.passCheckPattern == true {
                                        triggers.login.send()
                                    }
                                

                                }) {
                                    Text("立即登录")
                                        .font(Font.g_font_body_regular16)
                                        .foregroundColor(Color.g_color_font_white)
                                        .frame(width: 280, height: 40)
                                        .background(Color.g_color_theme)
                                        .cornerRadius(20)
                                }
                                Spacer()
                            }
                        }
                        .frame(maxHeight: .infinity, alignment: .top) // 确保元素在页面顶部
                       
                        // 提示消息
                        GToastFailure(isShowing: $output.showPattenFailure,text: $output.failureText)
                        GToastInfoLarge(isShowing: $output.showPattenFailureAll, text:$output.failureText)
                        if output.showLoginFailure {
                            // 遮罩层
                            Color.g_color_bg_mask
                                .edgesIgnoringSafeArea(.all)
                            PopupView(message: output.failureText, finish: {_ in
                                output.showLoginFailure = false
                            })
                        }
                        if output.showForgotPasswordSheet {
                            // 遮罩层
                            Color.g_color_bg_mask
                                .edgesIgnoringSafeArea(.all)
                                .onTapGesture {
                                    output.showForgotPasswordSheet = false
                                }
                            // 弹框
                            ForgotPasswordSheet(isPresented: $output.showForgotPasswordSheet)
                        }
                    }
                }
            }.environment(\.locale,.init(identifier: localeManager.localeLanguage)) //设置当前页面语言
        }

    
  
    

   
}

struct LoginView_Preview: PreviewProvider {
    static var previews: some View {
        LoginView(viewModel: LoginViewModel(navigationController: UINavigationController()))
    }
}

extension Container {
    func loginView(navigationController: UINavigationController) -> Factory<LoginView> {
        Factory(self) {
            LoginView(viewModel: LoginViewModel(navigationController: navigationController))
        }
    }
}
